package com.demo.service.impl;

import com.demo.dao.UserDiseaseDao;
import com.demo.entity.User;
import com.demo.entity.UserDisease;
import com.demo.service.UserDiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.List;

@Service
@Transactional
public class UserDiseaseServiceImpl implements UserDiseaseService {
    @Autowired
    private UserDiseaseDao userDiseaseDao;


    @Override
    public void handleUserDisease(User user, String dataModel, String diseaseStr, String eyeName) {
        if (!StringUtils.hasLength(diseaseStr)) {
            return;
        }
        List<UserDisease> userDiseaseList = new LinkedList<>();
        String[] diseaseArray = diseaseStr.split("、");
        for (String disease : diseaseArray
        ) {
            String diseaseName = StringUtils.trimAllWhitespace(disease);
            if (StringUtils.hasLength(diseaseName)) {
                UserDisease userDisease = new UserDisease();
                userDisease.setUserId(user.getId());
                userDisease.setDiseaseName(diseaseName);
                userDisease.setDataModel(dataModel);
                userDisease.setEyeType(eyeName);
                userDiseaseList.add(userDisease);
            }
        }
        userDiseaseDao.saveAll(userDiseaseList);
    }

    @Override
    public void deleteAll() {
        userDiseaseDao.deleteAll();
        userDiseaseDao.updateAutoIncrement(1);
    }
}
